package com.geping.etl.common.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.geping.etl.common.entity.Report_Info;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface Report_InfoRepository extends JpaRepository<Report_Info, Integer>, JpaSpecificationExecutor<Report_Info> {

    @Query("select g from Report_Info g where g.subjectId = ?1 order by g.code ASC")
    public List<Report_Info> getReportBySubjectId(String subjectId);

/*
    @Query("select g from Report_Info g where subjectId = ?1 and g.instruction = ?2 order by id ASC")
    public List<Report_Info> getReportBySubjectId8(String subjectId, String instruction);
*/

    //修改待提交数量
    @Modifying
    @Query("update Report_Info g set g.pending = :#{#ri.pending} where g.code = :#{#ri.code}")
    public void updateReportInfoPending(@Param("ri") Report_Info ri);

    //修改待审核数量
    @Modifying
    @Query("update Report_Info g set g.pendingAudit = :#{#ri.pendingAudit} where g.code = :#{#ri.code}")
    public void updateReportInfoPendingAudit(@Param("ri") Report_Info ri);

    //修改审核不通过数量
    @Modifying
    @Query("update Report_Info g set g.checkFail = :#{#ri.checkFail} where g.code = :#{#ri.code}")
    public void updateReportInfoCheckFail(@Param("ri") Report_Info ri);

    //修改审核通过数量
    @Modifying
    @Query("update Report_Info g set g.checkSuccess = :#{#ri.checkSuccess},g.noMessageName = :#{#ri.noMessageName} where g.code = :#{#ri.code}")
    public void updateReportInfoCheckSuccess(@Param("ri") Report_Info ri);

    //存储分组信息
    @Transactional
    @Modifying
    @Query("update Report_Info r set r.updateTime = ?2 where r.code = ?1 and r.subjectId = ?3 ")
    void saveGroupInfo(String code, String info, String subjectId);

    @Transactional
    @Modifying
    @Query("update Report_Info r set r.updateTime = ?2 where r.code = ?1 and r.subjectId = ?3 and r.instruction = ?4 ")
    void saveGroupInfo8(String code, String info, String subjectId, String instruction);


    //获取最大ID数
    @Query("select max(s.id) from Report_Info s")
    public Integer findMaxId();

    @Modifying
    @Query("delete from Report_Info   where code = ?1  ")
    int deleteReport(String code);

    //存储分组信息
    @Transactional
    @Modifying
    @Query("update Report_Info r set r.instruction = ?3, r.uploadFrequency = ?4, r.reportStatus = ?5 where r.code = ?1 and r.name = ?2 ")
    void updateReportInfoInstruction(String code, String name, String instruction, String uploadFrequency, String reportStatus);

}
